package com.hqyj.controller;

import com.hqyj.pojo.Building;
import com.hqyj.pojo.Employee;
import com.hqyj.pojo.PageData;
import com.hqyj.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/building")
public class BuildingController {
    @Autowired
    private BuildingService buildingService;

    //获取楼房信息
    /*@RequestMapping("/getBuilding")
    public String getAll(ModelMap modelMap) {
        List<Building> all = buildingService.getAllBuilding();
        System.out.println(all);
        modelMap.put("BUILDING", all);
        return "building/list";
    }*/

    //修改楼房信息
    @RequestMapping("/updateBuilding")
    @ResponseBody
    public Map<String, String> updateBuilding(Building building) {
        int i = buildingService.updateBuilding(building);
        Map<String, String> map = new HashMap<>();
        if (i != 0) {
            map.put("msg", "ok");
        } else {
            map.put("msg", "no");

        }
        return map;
    }

    //删除楼房信息
    @RequestMapping("/delBuilding")
    public String delBuilding(Integer pageNumber, Integer pageSize,ModelMap modelMap, Building buildId) {
        buildingService.deleteBuliding(buildId);
        int number;
        int size;
        if (pageNumber == null) {
            number = 1; //默认显示第1页
        } else {
            number = pageNumber;
        }
        if (pageSize == null) {
            size = 5; //默认每页显示两条
        } else {
            size = pageSize;
        }
        //调用服务层换回模型数据
        PageData<Building> pageData = buildingService.getBuildingByPage(number, size);
        //给视图传递上下文参数
        modelMap.put("BUILDING", pageData);
        return "building/list";
    }

    //获取楼房信息及分页功能
    @RequestMapping("/getBuilding")
    public String getAllBuildingByPage(Integer pageNumber, Integer pageSize, ModelMap modelMap) {
        //处理参数
        int number;
        int size;
        if (pageNumber == null) {
            number = 1; //默认显示第1页
        } else {
            number = pageNumber;
        }
        if (pageSize == null) {
            size = 5; //默认每页显示两条
        } else {
            size = pageSize;
        }
        //调用服务层换回模型数据
        PageData<Building> pageData = buildingService.getBuildingByPage(number, size);
        //给视图传递上下文参数
        modelMap.put("BUILDING", pageData);
        return "building/list";
    }
}
